import Vue from "vue";
import Vuex from "vuex";
Vue.use(Vuex);
const store = new Vuex.Store({
  state: {
    vuex_user: uni.getStorageSync("vuex_user")
      ? uni.getStorageSync("vuex_user")
      : {},
    vuex_token: uni.getStorageSync("vuex_token")
      ? uni.getStorageSync("vuex_token")
      : "",
    vuex_refresh_token: uni.getStorageSync("vuex_refresh_token")
      ? uni.getStorageSync("vuex_refresh_token")
      : "",
    vuex_config: {},
    vuex_webs: uni.getStorageSync("vuex_webs")
      ? uni.getStorageSync("vuex_webs")
      : {},
    pre_login_page: uni.getStorageSync("pre_login_page")
      ? uni.getStorageSync("pre_login_page")
      : "",
    from_invite_code: uni.getStorageSync("from_invite_code")
      ? uni.getStorageSync("from_invite_code")
      : "",
  },
  mutations: {
    set(state, data) {
      let nameArr = data.name.split(".");
      let saveKey = "";
      let len = nameArr.length;
      if (len >= 2) {
        let obj = state[nameArr[0]];
        for (let i = 1; i < len - 1; i++) {
          obj = obj[nameArr[i]];
        }
        obj[nameArr[len - 1]] = data.value;
        saveKey = nameArr[0];
      } else {
        state[data.name] = data.value;
        saveKey = data.name;
      }
      uni.setStorageSync(saveKey, state[saveKey]);
    },
  },
});
export default store;
